package com.irdstudio.bhcredit.core.batch.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据表操作类-[表名: 02.批次任务配置(BAT_TASK_UNIT_CONFIG)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-05-04
 */
public class BatTaskUnitConfigDao {

	/* 连接对象 */
	Connection conn = null;	

	public BatTaskUnitConfigDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public BatTaskUnitConfig queryWithKeys(String taskId)
			throws SQLException {
		BatTaskUnitConfig dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM BAT_TASK_UNIT_CONFIG WHERE task_id=?");
			ps.setString(1,taskId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new BatTaskUnitConfig();
				dc.setTaskId(rs.getString("task_id"));
				dc.setTaskName(rs.getString("task_name"));
				dc.setPreviousTaskId(rs.getString("previous_task_id"));
				dc.setBatchId(rs.getString("batch_id"));
				dc.setStageId(rs.getString("stage_id"));
				dc.setLocaleId(rs.getString("locale_id"));
				dc.setPluginType(rs.getString("plugin_type"));
				dc.setPluginId(rs.getInt("plugin_id"));
				dc.setPluginSourceType(rs.getString("plugin_source_type"));
				dc.setPluginParaFlag(rs.getString("plugin_para_flag"));
				dc.setTaskPri(rs.getString("task_pri"));
				dc.setTaskRunType(rs.getString("task_run_type"));
				dc.setCycleInteval(rs.getInt("cycle_inteval"));
				dc.setMaxWaitTime(rs.getInt("max_wait_time"));
				dc.setTaskCycleType(rs.getString("task_cycle_type"));
				dc.setTaskCronValue(rs.getString("task_cron_value"));
				dc.setTaskDelayTime(rs.getInt("task_delay_time"));
				dc.setMaxRunCount(rs.getInt("max_run_count"));
				dc.setTaskSkipTactic(rs.getString("task_skip_tactic"));
				dc.setAgainRunSpace(rs.getInt("again_run_space"));
				dc.setTaskEstimateTime(rs.getInt("task_estimate_time"));
				dc.setTaskTimeoutTime(rs.getInt("task_timeout_time"));
				dc.setTaskTimeoutTactic(rs.getString("task_timeout_tactic"));
				dc.setTaskUseState(rs.getString("task_use_state"));
				dc.setTaskUseArea(rs.getString("task_use_area"));
				dc.setSubsDsCode(rs.getString("subs_ds_code"));
				dc.setRemark(rs.getString("remark"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatTaskUnitConfigWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<BatTaskUnitConfig> queryWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<BatTaskUnitConfig> dcList = new ArrayList<BatTaskUnitConfig>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM bat_task_unit_config "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				BatTaskUnitConfig dc = new BatTaskUnitConfig();
				dc.setTaskId(rs.getString("task_id"));
				dc.setTaskName(rs.getString("task_name"));
				dc.setPreviousTaskId(rs.getString("previous_task_id"));
				dc.setBatchId(rs.getString("batch_id"));
				dc.setStageId(rs.getString("stage_id"));
				dc.setLocaleId(rs.getString("locale_id"));
				dc.setPluginType(rs.getString("plugin_type"));
				dc.setPluginId(rs.getInt("plugin_id"));
				dc.setPluginSourceType(rs.getString("plugin_source_type"));
				dc.setPluginParaFlag(rs.getString("plugin_para_flag"));
				dc.setTaskPri(rs.getString("task_pri"));
				dc.setTaskRunType(rs.getString("task_run_type"));
				dc.setCycleInteval(rs.getInt("cycle_inteval"));
				dc.setMaxWaitTime(rs.getInt("max_wait_time"));
				dc.setTaskCycleType(rs.getString("task_cycle_type"));
				dc.setTaskCronValue(rs.getString("task_cron_value"));
				dc.setTaskDelayTime(rs.getInt("task_delay_time"));
				dc.setMaxRunCount(rs.getInt("max_run_count"));
				dc.setTaskSkipTactic(rs.getString("task_skip_tactic"));
				dc.setAgainRunSpace(rs.getInt("again_run_space"));
				dc.setTaskEstimateTime(rs.getInt("task_estimate_time"));
				dc.setTaskTimeoutTime(rs.getInt("task_timeout_time"));
				dc.setTaskTimeoutTactic(rs.getString("task_timeout_tactic"));
				dc.setTaskUseState(rs.getString("task_use_state"));
				dc.setTaskUseArea(rs.getString("task_use_area"));
				dc.setSubsDsCode(rs.getString("subs_ds_code"));
				dc.setRemark(rs.getString("remark"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatTaskUnitConfigWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
